package com.srit.source;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class Generate implements Source {
	protected String proPath;// 项目路径
	protected String sourcePath;// 代码路径
	protected String xmlPath;// xml配置路径
	protected String modual;// 模块名称
	protected String modualPath;// 模块路径
	protected String temPath;// 模板路径
	protected String beanPath;// bean路径
	protected String daoPath;// dao 路径
	protected String daoImplPath;// daoImpl路径
	protected String servicePath;// service路径
	protected String serviceImplPath;// serviceImpl路径
	protected String actionPath;// action 路径

	static Logger log = LoggerFactory.getLogger(Generate.class);

	public Generate() {
		try {
			this.proPath = new File(".").getCanonicalPath();
			this.sourcePath = this.proPath + SRC_MAIN_JAVA;
			this.xmlPath = this.proPath + SRC_MAIN_RESOURCES;
			this.temPath = this.proPath + TEM;
			log.debug("proPath is {}", proPath);
			log.debug("sourcePath is {}", sourcePath);
			log.debug("xmlPath is {}", xmlPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setModual(String modual) {
		this.modual = modual;
		this.modualPath = sourcePath
				+ MODEL_PACKAGE.replace("*", modual).replace(".", "/");
		this.beanPath = sourcePath
				+ BEAN_PACKAGE.replace(".", "/").replace("*", modual);
		this.daoPath = sourcePath
				+ DAO_PACKAGE.replace(".", "/").replace("*", modual);
		this.daoImplPath = this.daoPath + "/impl";
		this.servicePath = sourcePath
				+ SERVICE_PACKAGE.replace(".", "/").replace("*", modual);
		this.serviceImplPath = this.servicePath + "/impl";
		this.actionPath = sourcePath
				+ ACTION_PACKAGE.replace(".", "/").replace("*", modual);

		log.debug("daoImplPath is {}", daoImplPath);
		log.debug("serviceImplPath is {}", serviceImplPath);
		log.debug("actionPath is {}", actionPath);
	}

	/**
	 * 模块中所有bean名称
	 * 
	 * @return
	 */
	protected List<File> listBeansInPackage() {
		return listFilesInDir(beanPath);
	}

	/**
	 * 显示文件夹中所有文件
	 * 
	 * @param dirPath
	 * @return
	 */
	protected List<File> listFilesInDir(String dirPath) {
		File dir = new File(dirPath);
		List<File> fileList = new ArrayList<File>();
		String[] files = dir.list();
		for (int i = 0; i < files.length; i++) {
			// System.out.println(dirPath + "/" + files[i]);
			fileList.add(new File(dirPath + "/" + files[i]));
		}
		return fileList;
	}
}
